#!/usr/bin/env python
# encoding: utf-8


"""
@file: sanjiaoxing_zhouchang.py
@time: 2017/4/28 下午4:03
"""
# 求三角形周长
from mathsolver.functions.base import *
from sympy import symbols
from mathsolver.functions.sanjiao.sanjiaoxing_zuizhi import Triangle2TrigF


# 求周长表达式的
class ZhouChang001(BaseFunction):
    def search_single_value(self, symb):
        _v = self.search(symb)
        if isinstance(_v, FiniteSet) and len(_v) == 1:
            return list(_v)[0]
        else:
            return _v

    def solver(self, *args):
        f_expr = sympify('a+b+c')
        self.steps.append(['由题意可知周长=', new_latex(f_expr)])
        _solver = Triangle2TrigF(known=self.known).solver(BasePoly(f_expr))
        self.steps.extend(_solver.steps)
        per_expr = _solver.output[0].sympify()
        symb_intl = _solver.output[1].sympify().values()[0]
        _A, _B, _C = symbols('A, B, C')
        for angle in [_A, _B, _C]:
            angle_value = self.search_single_value(angle)
            if angle_value:
                per_expr = per_expr.subs(angle, angle_value)
        if len(per_expr.free_symbols) > 1:
            raise Exception('Can not solve the question')
        else:
            self.steps.append(['  =', new_latex(per_expr)])
            symb = list(per_expr.free_symbols)[0]
            self.output.append(BasePoly(per_expr))
            self.output.append(BaseSymbolValue({symb: symb_intl}))
            return self


# 求三角形周长的
# Paramer1
class SanJiaoXingZhouChang(BaseFunction):
    CLS = [ZhouChang001, ]

    def solver(self, *args):
        solve_r = None
        for cl in SanJiaoXingZhouChang.CLS:
            try:
                solve_r = cl(known=self.known).solver(*args)
            except Exception:
                pass
        if not solve_r:
            raise Exception('Can not solve the Triangle circumference')
        return solve_r


if __name__ == '__main__':
    pass
